package com.example.yixueguan.views;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.yixueguan.R;
import com.example.yixueguan.bean.Student;
import com.example.yixueguan.database.DBHelper;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView mIvFriends;
    private ImageView mIvCourses;
    private ImageView mIvMine;
    private Fragment mContentFragment;
    private FragmentManager mFragmentManager;
    private FriendsFragment mFriendsFragment;
    private CoursesFragment mCoursesFragment;
    private MineFragment mMineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIvFriends = findViewById(R.id.iv_friends);
        mIvCourses = findViewById(R.id.iv_courses);
        mIvMine = findViewById(R.id.iv_mine);
        mIvFriends.setOnClickListener(this);
        mIvCourses.setOnClickListener(this);
        mIvMine.setOnClickListener(this);
        initFragment();
        changeFragment(mFriendsFragment);
        mFragmentManager.beginTransaction().add(R.id.fragment_content, mFriendsFragment).commitAllowingStateLoss();
        mContentFragment = mFriendsFragment;
        mIvFriends.setSelected(true);
        List<Student> list = DBHelper.getInstance(this).selectAllStudent();
        Log.d("", "LIST ==== " + list.size());
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_friends) {
            //点击friends图标，friends图标设置为选中状态，
            //courses和mine图标设置为非选中状态，选中状态图标颜色发生变化，
            //图标在drawable中的selector文件配置。
            mIvFriends.setSelected(true);
            mIvCourses.setSelected(false);
            mIvMine.setSelected(false);
            changeFragment(mFriendsFragment);
        } else if (id == R.id.iv_courses) {
            mIvFriends.setSelected(false);
            mIvCourses.setSelected(true);
            mIvMine.setSelected(false);
            changeFragment(mCoursesFragment);
        } else if (id == R.id.iv_mine) {
            mIvFriends.setSelected(false);
            mIvCourses.setSelected(false);
            mIvMine.setSelected(true);
            changeFragment(mMineFragment);
        }
    }

    private void initFragment() {
        mFragmentManager = getSupportFragmentManager();
        mFriendsFragment = new FriendsFragment();
        mCoursesFragment = new CoursesFragment();
        mMineFragment = new MineFragment();
    }

    public void changeFragment(Fragment to) {
        if (mContentFragment != null && mContentFragment != to) {
            if (!to.isAdded()) {
                mFragmentManager.beginTransaction().hide(mContentFragment).add(R.id.fragment_content, to).commitAllowingStateLoss();
            } else {
                mFragmentManager.beginTransaction().hide(mContentFragment).show(to).commitAllowingStateLoss();
            }
            mContentFragment = to;
        }
    }
}